﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meeting.DBUtility;
using System.Data;
using Newtonsoft.Json;
using System.Data.SqlClient;

namespace Meeting.BLL
{
    /// <summary>
    /// 模块作者：吴龙波
    /// 模块日期：20130116
    /// 模块功能：与角色相关的操作，角色创建，角色组的创建及关联
    /// </summary>
    public class Role
    {

        public string errorString = "";

        #region  角色相关操作
        //获取已经创建的角色
        public string GetRoleJson(int pageIndex, int pageSize, string filter)
        {
            int nAllRecord = 0, nAllPages = 0;
            DataTable dt = DbHelperSQL.GetPage("Id", "   *  ", " dbo.Role ", filter, "  ", pageIndex, pageSize, out nAllRecord, out nAllPages);
            if (dt == null) return "";
            Model.JsonGrid jr = new Model.JsonGrid();
            jr.total = nAllRecord;
            jr.rows = dt;
            return JsonConvert.SerializeObject(jr);
        }
        //角色编辑
        public bool RoleEdit(Model.Role model)
        {
            try
            {
                if (RoleExists(model.Id))
                {
                    return RoleUpdate(model);
                }
                else
                {
                    return RoleAdd(model);
                }
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return false;
            }
        }

        //判断是否汆子
        public bool RoleExists(string Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Role");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.NVarChar,128)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
         
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool RoleAdd(Meeting.Model.Role model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Role(");
            strSql.Append("Id,Name,Comment,IsActive)");
            strSql.Append(" values (");
            strSql.Append("@Id,@Name,@Comment,@IsActive)");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.NVarChar,128),
					new SqlParameter("@Name", SqlDbType.NVarChar,128),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@IsActive", SqlDbType.Bit,1)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Comment;
            parameters[3].Value = model.IsActive;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool RoleUpdate(Meeting.Model.Role model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Role set ");
            strSql.Append("Name=@Name,");
            strSql.Append("Comment=@Comment,");
            strSql.Append("IsActive=@IsActive");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,128),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@IsActive", SqlDbType.Bit,1),
					new SqlParameter("@Id", SqlDbType.NVarChar,128)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Comment;
            parameters[2].Value = model.IsActive;
            parameters[3].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
